package com.partner.control;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.partner.domain.ArticleShoucang;
import com.partner.domain.article;
import com.partner.response.TestSkillResponse;
import com.partner.service.ArticleService;
import com.partner.util.IOUtil;

/**
 * 考试技巧的控制器
 * @author Jinshuan
 *
 */
@Controller
public class ActicleAction {
	ArticleService articleService = new ArticleService();
	//分页查询
	@RequestMapping("/selectSkill")
	public @ResponseBody TestSkillResponse selectSkillByPage(InputStream is){
		TestSkillResponse testSkillResponse = new TestSkillResponse();
		//要从客户端传来当前页还有是要查询什么类型的帖子kkk
		String result = IOUtil.inputStreamToStream(is);
		
		int pageNow = 1;
		String type = "1";//文章的类别。默认是考试技巧
		if(result!=null&&!("".equals(result))){
			String[] res = result.split(";");
			pageNow = Integer.parseInt(res[0]);//分号前面是当前页
			if(res.length>1){
			type = res[1];
			}
		}
		int pageSize = 4;
		if(!("1".equals(type))){
			pageSize = 7;
		}
		testSkillResponse = articleService.selectSkillByPage(pageSize, pageNow,type);
		
		System.out.println(pageNow+"--"+testSkillResponse.getSkills().size());
		
		return testSkillResponse;
	}
	
	//查询对应用户收藏过的帖子
	@RequestMapping("/selectArticleForUser")
	public @ResponseBody List<article> selectArticleForUser(InputStream is){//传来用户id和pageNow
		System.out.println("来查询收藏的文章--");
		String result = IOUtil.inputStreamToStream(is);
		List<article> articles = new ArrayList<article>();
		int pageNow = 1;
		String phone = "";
		if(result!=null&&!("".equals(result))){
			String[] res = result.split(";");
			phone = res[0];//分号前面是当前页
			if(res.length>1){
				pageNow = Integer.parseInt(res[1]);
			}
			return articleService.selectShouCangArticle(phone, pageNow, 7);
		}
		return null;
	}
	//增加一条文章的收藏记录
	@RequestMapping("/insertArticleShouCang")
	public @ResponseBody int insertArticleShouCang(InputStream is){
		String result = IOUtil.inputStreamToStream(is);
		if(result!=null&&!("".equals(result))){
			Gson gson = new Gson();
			ArticleShoucang articleShoucang = gson.fromJson(result, ArticleShoucang.class);
			return articleService.insertArticleShoucang(articleShoucang);
		}
		return -1;
	}
	
	@RequestMapping("/selectArticleShoucang")
	public @ResponseBody List<ArticleShoucang> selectArticleShoucang(InputStream is){
		List<ArticleShoucang> articleShoucangs = new ArrayList<ArticleShoucang>();
		String phone = IOUtil.inputStreamToStream(is);
		if(phone!=null&&!("".equals(phone))){
			return articleService.selectArticleShoucangsByUser(phone);
		}
		return articleShoucangs;
	}
}
